import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

interface User {
    id: string;
    username: string;
    roles: string[];
}

export const useAuthStore = defineStore('auth', () => {
    const user = ref<User | null>(null);
    const token = ref<string | null>(null);

    const isAuthenticated = computed(() => !!token.value);

    const hasRole = (roles: string | string[]) => {
        if (!user.value) return false;
        const requiredRoles = Array.isArray(roles) ? roles : [roles];
        return requiredRoles.some(role => user.value?.roles.includes(role));
    };

    const login = async (credentials: { username: string; password: string }) => {
        // 实现登录逻辑
    };

    const logout = async () => {
        user.value = null;
        token.value = null;
    };

    return {
        user,
        token,
        isAuthenticated,
        hasRole,
        login,
        logout
    };
}); 